package com.sanrenzu.chunbo.ui.vertical;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import com.sanrenzu.chunbo.R;
import com.sanrenzu.chunbo.ui.fenlei.FenLeiFragment;
import com.sanrenzu.chunbo.ui.foodagency.FoodAgencyFragment;
import com.sanrenzu.chunbo.ui.vertical.shop.ShopFragment;
import com.sanrenzu.chunbo.ui.wode.WoDeFragment;

public class MainActivity extends AppCompatActivity {

    private Context mContext=this;
    private Button btn_main_home;
    private Button btn_main_live;
    private Button btn_main_follow;
    private Button btn_main_mine;
    private Button btn_main_meishi;

    private FenLeiFragment fenleiFragment;
    private FoodAgencyFragment foodAgencyFragment;
    private WoDeFragment woDeFragment;
    private ShopFragment shopFragment;
    private FragmentManager manager;

    //定义选择碎片的下标
    private static final int SELECTED_SHOUYE = 0;
    private static final int SELECTED_FENLEI = 1;
    private static final int SELECTED_MEISHI = 2;
    private static final int SELECTED_GOUWUCHE = 3;
    private static final int SELECTED_WODE =4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        initFragment();

        selectFragment(SELECTED_SHOUYE);
    }

    private void initView() {
        btn_main_home= (Button) findViewById(R.id.btn_main_home);
        btn_main_live= (Button) findViewById(R.id.btn_main_live);
        btn_main_follow= (Button) findViewById(R.id.btn_main_follow);
        btn_main_mine= (Button) findViewById(R.id.btn_main_mine);
        btn_main_meishi= (Button) findViewById(R.id.btn_main_meishi);

        btn_main_home.setSelected(true);
        btn_main_home.setTextColor(Color.parseColor("#32BE6E"));
        btn_main_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_main_home.setSelected(true);
                btn_main_live.setSelected(false);
                btn_main_follow.setSelected(false);
                btn_main_mine.setSelected(false);
                btn_main_meishi.setSelected(false);

                btn_main_home.setTextColor(Color.parseColor("#32BE6E"));
                btn_main_live.setTextColor(Color.parseColor("#000000"));
                btn_main_follow.setTextColor(Color.parseColor("#000000"));
                btn_main_mine.setTextColor(Color.parseColor("#000000"));
                btn_main_meishi.setTextColor(Color.parseColor("#000000"));
                selectFragment(SELECTED_SHOUYE);
            }
        });
        btn_main_live.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_main_home.setSelected(false);
                btn_main_live.setSelected(true);
                btn_main_follow.setSelected(false);
                btn_main_mine.setSelected(false);
                btn_main_meishi.setSelected(false);

                btn_main_home.setTextColor(Color.parseColor("#000000"));
                btn_main_live.setTextColor(Color.parseColor("#32BE6E"));
                btn_main_follow.setTextColor(Color.parseColor("#000000"));
                btn_main_mine.setTextColor(Color.parseColor("#000000"));
                btn_main_meishi.setTextColor(Color.parseColor("#000000"));
                selectFragment(SELECTED_FENLEI);
            }
        });

        btn_main_meishi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_main_home.setSelected(false);
                btn_main_live.setSelected(false);
                btn_main_follow.setSelected(false);
                btn_main_mine.setSelected(false);
                btn_main_meishi.setSelected(true);

                btn_main_home.setTextColor(Color.parseColor("#000000"));
                btn_main_live.setTextColor(Color.parseColor("#000000"));
                btn_main_follow.setTextColor(Color.parseColor("#000000"));
                btn_main_mine.setTextColor(Color.parseColor("#000000"));
                btn_main_meishi.setTextColor(Color.parseColor("#32BE6E"));
                selectFragment(SELECTED_MEISHI);
            }
        });
        btn_main_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_main_home.setSelected(false);
                btn_main_live.setSelected(false);
                btn_main_follow.setSelected(true);
                btn_main_mine.setSelected(false);
                btn_main_meishi.setSelected(false);

                btn_main_home.setTextColor(Color.parseColor("#000000"));
                btn_main_live.setTextColor(Color.parseColor("#000000"));
                btn_main_follow.setTextColor(Color.parseColor("#32BE6E"));
                btn_main_mine.setTextColor(Color.parseColor("#000000"));
                btn_main_meishi.setTextColor(Color.parseColor("#000000"));
                selectFragment(SELECTED_GOUWUCHE);
            }
        });
        btn_main_mine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_main_home.setSelected(false);
                btn_main_live.setSelected(false);
                btn_main_follow.setSelected(false);
                btn_main_mine.setSelected(true);
                btn_main_meishi.setSelected(false);

                btn_main_home.setTextColor(Color.parseColor("#000000"));
                btn_main_live.setTextColor(Color.parseColor("#000000"));
                btn_main_follow.setTextColor(Color.parseColor("#000000"));
                btn_main_mine.setTextColor(Color.parseColor("#32BE6E"));
                btn_main_meishi.setTextColor(Color.parseColor("#000000"));
                selectFragment(SELECTED_WODE);
            }
        });
    }


    private void initFragment() {
        //初始化碎片
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fenleiFragment = new FenLeiFragment();
       foodAgencyFragment = new FoodAgencyFragment();
        shopFragment = new ShopFragment();
        woDeFragment = new WoDeFragment();
        //将每个碎片存到事务中
        transaction.add(R.id.layout_container, fenleiFragment);
        transaction.add(R.id.layout_container,foodAgencyFragment);
        transaction.add(R.id.layout_container,shopFragment);
        transaction.add(R.id.layout_container,woDeFragment);
        //展现
        transaction.commit();
    }

    //设置选择的碎片下标
    private void selectFragment(int position) {
        //开启事务
        FragmentTransaction transaction = manager.beginTransaction();
        //将所有的碎片先隐藏
        transaction.hide(fenleiFragment).hide(foodAgencyFragment).hide(shopFragment).hide(woDeFragment);
        switch (position) {
            //根据选择的小标，显示碎片
            case SELECTED_SHOUYE:
                break;
            case SELECTED_FENLEI:
                transaction.show(fenleiFragment);
                break;
            case SELECTED_MEISHI:
                transaction.show(foodAgencyFragment);
                break;
            case SELECTED_GOUWUCHE:
                transaction.show(shopFragment);
                break;
            case SELECTED_WODE:
                transaction.show(woDeFragment);
                break;
        }
        transaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//退出窗口
        if (keyCode==KeyEvent.KEYCODE_BACK){
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("确认退出？");
            builder.setNegativeButton("取消", null);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.this.finish();
                }
            });
            builder.show();
        }
        return super.onKeyDown(keyCode, event);
    }
}
